package com.easymin.weixin.controller.res;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.common.api.PassengerApi;
import com.easymin.weixin.common.api.SalesApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.entity.passenger.Passenger;
import com.easymin.weixin.manager.WxApplicationManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/daijia")
public class DaijiaOrderRes {

    private static final Logger logger = LoggerFactory.getLogger(DaijiaApi.class);

    @Autowired
    private DaijiaApi daijiaApi;
    @Autowired
    private PassengerApi passengerApi;
    @Autowired
    private SalesApi salesApi;
    @Autowired
    private WxApplicationManager wxApplicationManager;

    /**
     * 创建代驾订单
     *
     * @param passengerId        客户ID
     * @param passengerName      客户名称
     * @param passengerPhone     客户手机号
     * @param userPhone          帮别人下时别人的电话
     * @param companyId          公司ID
     * @param channelName        下单渠道名称
     * @param startAddress       开始地址
     * @param startAddressDetail 开始详细地址 false
     * @param startLat           开始地址lat
     * @param startLng           开始地址lng
     * @param endAddress         结束地址 false
     * @param endAddressDetail   结束详细地址 false
     * @param endLat             结束地址lat false
     * @param endLng             结束地址lng false
     * @param serverTime         服务时间
     * @param budgetPay          预算费用MONEY false
     * @param couponId           优惠券ID couponId
     * @param startPrice         起步价 false
     * @param mileagePrice       公里价 false
     * @param runTimePrice       时间费用 false
     * @param travelTime         行程费用 false
     * @param mileage            公里数 false
     * @param appKey             系统key
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = {"create"}, method = RequestMethod.POST)
    public Map<String, Object> createOrder(@RequestParam("passengerId") Long passengerId,
                                           @RequestParam("passengerName") String passengerName, @RequestParam("passengerPhone") String passengerPhone,
                                           @RequestParam("userPhone") String userPhone, @RequestParam("companyId") Long companyId,
                                           @RequestParam("channelName") String channelName, @RequestParam("startAddress") String startAddress,
                                           @RequestParam(required = false, value = "startAddressDetail") String startAddressDetail,
                                           @RequestParam("startLat") Double startLat, @RequestParam("startLng") Double startLng,
                                           @RequestParam(required = false, value = "endAddress") String endAddress, //
                                           @RequestParam(required = false, value = "endAddressDetail") String endAddressDetail,
                                           @RequestParam(required = false, value = "endLat") Double endLat, //
                                           @RequestParam(required = false, value = "endLng") Double endLng, //
                                           @RequestParam("serverTime") Long serverTime,
                                           @RequestParam(required = false, value = "budgetPay") Double budgetPay, //
                                           @RequestParam(required = false, value = "couponId") Long couponId,
                                           @RequestParam(required = false, value = "startPrice") Double startPrice,
                                           @RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
                                           @RequestParam(required = false, value = "runTimePrice") Double runTimePrice,
                                           @RequestParam(required = false, value = "travelTime") Integer travelTime,
                                           @RequestParam(required = false, value = "mileage") Double mileage,
                                           @RequestParam(required = false, value = "orderNoRenBao") String orderNoRenBao,
                                           @RequestParam(required = false, value = "cuponNoRenBao") String cuponNoRenBao,
                                           @RequestParam(required = false, value = "preferentialType") String preferentialType,
                                           @RequestParam("appKey") String appKey)

            throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            Passenger passenger = passengerApi.findById(passengerId, appKey);
            if (null == passenger) {
                map.put("code", -20);
                map.put("msg", "客户不存在");
                return map;
            }
            if (passenger.getInBlackList()) {
                map.put("code", -6);
                map.put("msg", "黑名单客户不允许下单");
                return map;
            }
            map = daijiaApi.create(wxApplication.getAddress(), passengerId, passengerName, passengerPhone, userPhone,
                    companyId, channelName, startAddress, startAddressDetail, startLat, startLng, endAddress,
                    endAddressDetail, endLat, endLng, serverTime, budgetPay, couponId, orderNoRenBao,
                    cuponNoRenBao, preferentialType);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", ex.getMessage());
        }
        return map;
    }

    /**
     * 创建人保代驾订单
     *
     * @param passengerId        客户ID
     * @param passengerName      客户名称
     * @param passengerPhone     客户手机号
     * @param userPhone          帮别人下时别人的电话
     * @param companyId          公司ID
     * @param channelName        下单渠道名称
     * @param startAddress       开始地址
     * @param startAddressDetail 开始详细地址 false
     * @param startLat           开始地址lat
     * @param startLng           开始地址lng
     * @param endAddress         结束地址 false
     * @param endAddressDetail   结束详细地址 false
     * @param endLat             结束地址lat false
     * @param endLng             结束地址lng false
     * @param serverTime         服务时间
     * @param budgetPay          预算费用MONEY false
     * @param couponId           优惠券ID couponId
     * @param startPrice         起步价 false
     * @param mileagePrice       公里价 false
     * @param runTimePrice       时间费用 false
     * @param travelTime         行程费用 false
     * @param mileage            公里数 false
     * @param appKey             系统key
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = {"createRenBao"}, method = RequestMethod.POST)
    public Map<String, Object> createRenBaoOrder(@RequestParam(required = false, value = "passengerId") Long passengerId,
                                           @RequestParam(required = false, value = "passengerName") String passengerName, @RequestParam(required = false, value ="passengerPhone") String passengerPhone,
                                           @RequestParam(required = false, value = "userPhone") String userPhone, @RequestParam(required = false, value ="companyId") Long companyId,
                                           @RequestParam(required = false, value = "channelName") String channelName, @RequestParam(required = false, value ="startAddress") String startAddress,
                                           @RequestParam(required = false, value = "startAddressDetail") String startAddressDetail,
                                           @RequestParam(required = false, value = "startLat") Double startLat, @RequestParam(required = false, value ="startLng") Double startLng,
                                           @RequestParam(required = false, value = "endAddress") String endAddress, //
                                           @RequestParam(required = false, value = "endAddressDetail") String endAddressDetail,
                                           @RequestParam(required = false, value = "endLat") Double endLat, //
                                           @RequestParam(required = false, value = "endLng") Double endLng, //
                                           @RequestParam(required = false, value ="serverTime") Long serverTime,
                                           @RequestParam(required = false, value = "budgetPay") Double budgetPay, //
                                           @RequestParam(required = false, value = "couponId") Long couponId,
                                           @RequestParam(required = false, value = "startPrice") Double startPrice,
                                           @RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
                                           @RequestParam(required = false, value = "runTimePrice") Double runTimePrice,
                                           @RequestParam(required = false, value = "travelTime") Integer travelTime,
                                           @RequestParam(required = false, value = "mileage") Double mileage,
                                           @RequestParam(required = false, value = "orderNoRenBao") String orderNoRenBao,
                                           @RequestParam(required = false, value = "cuponNoRenBao") String cuponNoRenBao,
                                           @RequestParam(required = false, value = "preferentialType") String preferentialType,
                                           @RequestParam(required = false, value = "passengerType") String passengerType,
                                           @RequestParam("appKey") String appKey)

            throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            Passenger passenger = passengerApi.findById(passengerId, appKey);
            if (null == passenger) {
                map.put("code", -20);
                map.put("msg", "客户不存在");
                return map;
            }
            if (passenger.getInBlackList()) {
                map.put("code", -6);
                map.put("msg", "黑名单客户不允许下单");
                return map;
            }
            map = daijiaApi.createRenBao(wxApplication.getAddress(), passengerId, passengerName, passengerPhone, userPhone,
                    companyId, channelName, startAddress, startAddressDetail, startLat, startLng, endAddress,
                    endAddressDetail, endLat, endLng, serverTime, budgetPay, couponId, orderNoRenBao,
                    cuponNoRenBao, preferentialType,passengerType);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "人保订单创建失败");
        }
        return map;
    }


    /**
     * 分页查询单个乘客的订单.
     */
    @RequestMapping(value = {"findOrders"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findOrders(@RequestParam("passengerId") Long passengerId,
                                   @RequestParam("page") Integer page, @RequestParam("rows") Integer rows,
                                   @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.findOrders(wxApplication.getAddress(), passengerId, page, rows);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "查询订单失败");
        }
        return map;
    }

    /**
     * 查询单个订单.
     */
    @RequestMapping(value = {"findOne"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findOne(@RequestParam("orderId") Long orderId,
                                @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.findOne(wxApplication.getAddress(), orderId);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "查询订单失败");
        }
        return map;
    }

    /**
     * 获取预估价钱.
     */

    @RequestMapping(value = {"getPrice"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> getPrice(
            @RequestParam(required = false, value = "passengerId") Long passengerId,
            @RequestParam("startLat") Double startLat, @RequestParam("startLng") Double startLng,
            @RequestParam("endLat") Double endLat, @RequestParam("endLng") Double endLng,
            @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        if (startLat == null || startLat == 0 || startLng == null || startLng == 0) {
            map.put("code", -2);
            map.put("msg", "预约地位置不能为空");
            return map;
        }
        if (endLat == null || endLat == 0 || endLng == null || endLng == 0) {
            map.put("code", -3);
            map.put("msg", "目的地位置不能为空");
            return map;
        }
        if (null == passengerId) {
            passengerId = 0L;
        }
        try {
            Map<String, Object> map2 = daijiaApi.getDaijiaPrice(wxApplication.getAddress(), passengerId, startLat,
                    startLng, endLat, endLng);
            if (map2.get("success").equals(true)) {
                map.put("code", 0);
                map.put("msg", "成功");
                map.put("data", map2);
            } else {
                map.put("code", -4);
                map.put("msg", map2.get("message"));
            }
            return map;
        } catch (Exception ex) {
            logger.error("预算价格失败", ex.getMessage());
            map.put("code", 500);
            map.put("msg", "预算价格失败");
            return map;
        }
    }

    /**
     * 预估价钱(根据时间,距离预算).
     *
     * @param time        时间 分钟
     * @param mileage     距离 公里
     * @param passengerId 乘客Id
     * @param acKey       区分公司
     * @param startLng    任务起点经度
     * @param startLat    任务起点纬度
     */
    @RequestMapping(value = {"getNewPrice"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> getNewPrice(@RequestParam(required = false, value = "time") Integer time,
                                    @RequestParam(required = false, value = "mileage") Double mileage,
                                    @RequestParam(required = false, value = "passengerId") Long passengerId,
                                    @RequestParam(required = false, value = "acKey") String acKey,
                                    @RequestParam(required = false, value = "startLat") Double startLat,
                                    @RequestParam(required = false, value = "startLng") Double startLng,
                                    @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            Map<String, Object> map2 = daijiaApi.getDaijiaNewPrice(wxApplication.getAddress(), passengerId, acKey, time,
                    mileage, startLat, startLng);
            if (map2.get("success").equals(true)) {
                map.put("code", 0);
                map.put("msg", "成功");
                map.put("data", map2);
            } else {
                map.put("code", -4);
                map.put("msg", map2.get("message"));
            }
            return map;
        } catch (Exception ex) {
            logger.error("预算价格失败", ex);
            map.put("code", 500);
            map.put("msg", "预算价格失败");
            return map;
        }
    }

    /**
     * 查询司机地址.
     */

    @RequestMapping(value = {"getDriverPlace"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> getDriverPlace(@RequestParam("employId") Long employId,
                                       @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.getDriverPlace(wxApplication.getAddress(), employId);
        } catch (Exception ex) {
            logger.error("查询司机地址", ex.getMessage());
            map.put("code", 500);
            map.put("msg", "查询司机地址失败");
        }
        return map;
    }

    /**
     * 代驾销单.
     */

    @RequestMapping(value = {"cancel"}, method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> cancel(@RequestParam("orderId") Long orderId,
                               @RequestParam("cause") String cause, @RequestParam("appKey") String appKey) {
        appKey = StringUtils.trimToEmpty(appKey);
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.cancel(wxApplication.getAddress(), orderId, cause);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "内部服务器错误");
        }
        return map;
    }

    /**
     * 评价订单
     *
     * @param phone    乘客电话
     * @param driverId 司机ID
     * @param orderId  订单ID
     * @param score    评价分数
     * @param content  评价内容
     * @param appKey   系统key
     * @return
     */
    @RequestMapping(value = {"review"}, method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> review(@RequestParam("phone") String phone,
                               @RequestParam("driverId") Long driverId, @RequestParam("orderId") Long orderId,
                               @RequestParam("score") Double score, @RequestParam(required = false, value = "content") String content,
                               @RequestParam("appKey") String appKey) {
        appKey = StringUtils.trimToEmpty(appKey);
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.review(wxApplication.getAddress(), phone, driverId, orderId, score, content);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "内部服务器错误");
        }
        return map;
    }

    /**
     * 代驾催单.
     */

    @RequestMapping(value = {"reminder"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> hastenDaijiaOrder(@RequestParam("orderId") Long orderId,
                                          @RequestParam("appKey") String appKey) {
        appKey = StringUtils.trimToEmpty(appKey);
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.hastenDaijiaOrder(wxApplication.getAddress(), orderId);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "内部服务器错误");
        }
        return map;
    }

    /**
     * 支付宝支付接口.
     */

    @RequestMapping(value = {"aliprepay"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> aliprepay(@RequestParam("orderId") Long orderId,
                                  @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.daijiaaliPrepay(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "支付接口失败");
            return map;
        }
    }

    /**
     * 微信支付接口.
     */

    @RequestMapping(value = {"wxprepay"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> wxprepay(@RequestParam("orderId") Long orderId,
                                 @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.daijiawxPrepay(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "支付接口失败");
            return map;
        }
    }

    /**
     * 代驾的余额签单预支付.
     *
     * @param orderId     临时订单Id
     * @param passengerId 客户id
     * @param payType     签单:sign 余额:balance
     */
    @RequestMapping(value = {"balanceSignPay"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> balanceSignPay(@RequestParam("orderId") Long orderId,
                                       @RequestParam("passengerId") Long passengerId, @RequestParam("payType") String payType,
                                       @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.balanceSignPayDaijia(wxApplication.getAddress(), orderId, passengerId, payType);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "内部服务器失败");
            return map;
        }
    }

    /**
     * 代驾订单结算页面的数据.
     */
    @RequestMapping(value = {"completeTask"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> completeTask(@RequestParam("orderId") Long orderId,
                                     @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.completeTaskDaijia(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "获取结算页面的数据失败");
            return map;
        }
    }

    /**
     * 代驾订单客户端支付所需数据.
     */
    @RequestMapping(value = {"clientPayData"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> clientPayData(@RequestParam("orderId") Long orderId,
                                      @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.clientPayDataDaijia(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "获取结算页面的数据失败");
            return map;
        }
    }

    /**
     * 客户端支付宝结算代驾订单.
     */
    @RequestMapping(value = {"aliAfterPay"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> aliAfterpay(@RequestParam("orderId") Long orderId,
                                    @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.aliAfterpayDaijia(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "获取结算页面的数据失败");
            return map;
        }
    }

    /**
     * 客户端微信结算代驾订单.
     */
    @RequestMapping(value = {"wxAfterPay"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> wxAfterpay(@RequestParam("orderId") Long orderId,
                                   @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.wxAfterpayDaijia(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "获取结算页面的数据失败");
            return map;
        }
    }

    /**
     * 确认代驾订单费用的接口.
     *
     * @param orderId        订单id
     * @param discount       折扣
     * @param realCash       实付金额
     * @param guoluMoney     过路费
     * @param yuanchengMoney 远程费
     * @param otherMoney     其他费
     * @param mileage        里程
     * @param waitedTime     等待时间
     * @param travelTime     行驶时间
     * @param xml            保单数据
     * @param toPlace        实际目的地
     * @param toPlaceLng     目的地详情经度
     * @param toPlaceLat     目的地详情纬度
     */
    @RequestMapping(value = {"confirmTask"}, method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> complete(@RequestParam("orderId") Long orderId,
                                 @RequestParam(required = false, value = "discount") Double discount,
                                 @RequestParam("realCash") Double realCash, @RequestParam("guoluMoney") Double guoluMoney,
                                 @RequestParam("yuanchengMoney") Double yuanchengMoney, @RequestParam("otherMoney") Double otherMoney,
                                 @RequestParam("mileage") Double mileage, @RequestParam("waitedTime") Integer waitedTime,
                                 @RequestParam("travelTime") Integer travelTime,
                                 @RequestParam(required = false, value = "startPrice") Double startPrice,
                                 @RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
                                 @RequestParam(required = false, value = "runTimePrice") Double runTimePrice,
                                 @RequestParam(required = false, value = "waitedPrice") Double waitedPrice, @RequestParam("xml") String xml,
                                 @RequestParam(required = false, value = "toPlace") String toPlace,
                                 @RequestParam(required = false, value = "toPlaceLng") Double toPlaceLng,
                                 @RequestParam(required = false, value = "toPlaceLat") Double toPlaceLat,
                                 @RequestParam(required = false, value = "memo") String memo, @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.confirmTaskDaijia(wxApplication.getAddress(), orderId, discount, realCash, guoluMoney,
                    yuanchengMoney, otherMoney, mileage, waitedTime, travelTime, startPrice, mileagePrice, runTimePrice,
                    waitedPrice, xml, toPlace, toPlaceLng, toPlaceLat, memo);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "结算失败");
            return map;
        }
    }

    /**
     * 实时获取订单是否被结算.
     */
    @RequestMapping(value = {"checkTask"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> checkTask(@RequestParam("orderId") Long orderId,
                                  @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.checkTaskDaijia(wxApplication.getAddress(), orderId);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "实时获取订单是否被结算失败!");
            return map;
        }
    }

    /**
     * 最后结算接口.
     *
     * @param payType 支付类型: helppay(代付) sign(签单) balance(余额)
     */
    @RequestMapping(value = {"finishTask"}, method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> finishTask(@RequestParam("orderId") Long orderId,
                                   @RequestParam("payType") String payType, @RequestParam(required = false, value = "who") String who,
                                   @RequestParam("appKey") String appKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        try {
            map = daijiaApi.finishTaskDaijia(wxApplication.getAddress(), orderId, payType, who);
            return map;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            map.put("code", 500);
            map.put("msg", "结算提交失败!");
            return map;
        }
    }

}
